;( function( OPT )
{
    var PUBLIC = {

        init : function( OPT )
        {
            this.unload = OPT.unload || false;
            this.charSet = OPT.charSet || "utf-8";
            this.type = OPT.type || "text/javascript";
            this.lang = OPT.lang || "javascript";
            this.version = OPT.version || false;
            this.core = OPT.core || [];
            this.module = OPT.module || [];
            this.index = OPT.index || [];

            this._length = this.countListLength( this.core ) +
                this.countListLength( this.index ) +
                this.countListLength( this.module );

            this._Elemts = null;
            this._iNum = 0;
            this._timer = null;
            this._timer2 = null;

            this.startHandle();
        },

        countListLength : function( model )
        {
            if( model[ 0 ] instanceof Array  )
            {
                var total = 0;

                for( var i = 0, f; f = model[ i ]; i += 1 )
                {
                    total += f.length;
                }

                return total;
            }
            else
            {
                return model.length;
            };
        },

        /** 异步加载模块*/
        loadedHandle : function( url, callback )
        {
            var self = this;
            var oScript = document.createElement( "script" );
            var oTarget = document.body;
            var sCharSet = this.charSet;
            var sType = this.type;
            var sLang = this.lang;

            oScript._self = self;
            oScript._callback = callback || function(){};

            oScript.onload = oScript.onreadystatechange = oScript.onerror = function ()
            {
                if ( this && this.readyState && /^(?!(?:loaded|complete)$)/.test( this.readyState ) ) return false;

                this.onload = this.onreadystatechange = this.onerror = null;

                this.src = "";

                this.parentNode.removeChild( this );

                this._self._iNum += 1;

                typeof this._callback === "function" && this._callback .call( this._self );
            };

            oScript.charset = sCharSet;

            oScript.src = url + ( /\?/g.test( url ) ? "&" : "?" ) + ( this.version ? "_v=" + this.version : "n=" + new Date().getTime() );

            //oScript.src = url;

            oScript.type = sType;

            oScript.setAttribute( "language", sLang );

            try
            {
                oTarget.appendChild( oScript );
            }
            catch( e ) {};
        },

        handleQueue : function( item, callback )
        {
            var _Queue = item;

            var _step = 0;

            var _len = _Queue.length;

            while( _Queue.length )
            {
                var sPath = _Queue[ 0 ];

                this.loadedHandle( sPath + ".js", function()
                {
                    if( ++_step == _len )
                    {
                        typeof callback == "function" && callback.call( this );
                    }

                    this.setLoadBar();
                });

                _Queue.shift();
            }
        },

        handleItem  : function( item, callback )
        {
            var _item = item;

            var _step = 0;

            var _len = _item.length;

            while( _item.length )
            {
                this.handleQueue( _item[ _step ], function()
                {
                    if( ++_step == _len )
                    {
                        typeof callback == "function" && callback.call( this );
                    }
                });

                _item.shift();
            }
        },

        initLoadedBar : function()
        {
            if( this.unload ) return null;

            var owrap = top.document.getElementById("ad-js-init-wrap");
            var obar = top.document.getElementById("ad-js-init-bar");
            var otext = top.document.getElementById("ad-js-init-text");

            owrap.style.zIndex = 99;
            owrap.style.opacity = 1;

            return {
                wrap : owrap,
                bar : obar,
                text : otext
            }
        },

        setLoadBar : function()
        {
            if( !this._Elemts ) return false;

            var value = parseInt( this._iNum/this._length*100, 10  );

            this._Elemts.text.innerHTML = value;

            this._Elemts.bar.style.width = value + "%";
        },

        loadCompalte : function()
        {
            var self = this;
            var oElemts = this._Elemts;

            if( !this._Elemts ) return false;

            clearTimeout( self._timer );

            self._timer = setTimeout( function()
            {
                oElemts.wrap.style.opacity = 0;

                oElemts.wrap.style["-webkit-transition"] =
                    oElemts.wrap.style["-moz-transition"] =
                        oElemts.wrap.style["-ms-transition"] =
                            oElemts.wrap.style["-o-transition"] =
                                oElemts.wrap.style["transition"] = "opacity 0.5s ease";

                clearTimeout( self._timer2 );

                self._timer2 = setTimeout( function()
                {
                    oElemts.wrap.style.zIndex = -10;

                    oElemts.text.innerHTML = 0;

                    oElemts.bar.style.width = "0%";

                }, 500 );

            }, 1000 );
        },

        startHandle : function()
        {
            this._Elemts = this.initLoadedBar();

            this.handleItem( this.core, function()
            {
                this.handleItem( this.module, function()
                {
                    this.handleQueue( this.index, function()
                    {
                        this.loadCompalte();
                    });
                });

            });
        }

    };

    PUBLIC.init( OPT );

})( OPTIONS );